package com.kaiyidiannao.activity.messageInfo;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.*;
import com.kaiyi.diannao.net.AsyncHttpResponseParse;
import com.kaiyi.diannao.net.LoadDataHandler;
import com.kaiyi.diannao.net.RequestClient;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.common.util.UploadImageUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.data.Validate;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.imagescale.ImageGalleryActivity;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.view.ActionBarView;
import com.kaiyidiannao.widget.LoadingDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by 武 on 2014/5/26 0026.
 */
public class MessagePublish extends BaseActionBarActivity implements View.OnClickListener {
    private String types[] = new String[]{"招聘求职", "租房", "跳蚤市场", "生活服务", "教育培训", "婚恋交友", "医疗健康"};
    private LayoutInflater inflater;
    private TextView textView;
    private TextView text;
    private TextView jobs;
    private TextView house;
    private TextView market;
    private TextView life;
    private TextView education;
    private TextView love;
    private TextView health;
    private PopupWindow popupWindow;
    private ActionBarView actionBarView;
    private ImageView mImg0;
    private ImageView mImg1;
    private ImageView mImg2;
    private ImageView mImg3;
    private ImageView mImg4;
    private ImageView mImg5;
    private ImageView mImg6;
    private ImageView mImg7;
    private String[] mImagePaths = new String[8];
    private String[] items = new String[]{"选择本地图片", "拍照"};
    private LoadingDialog mLoadingDialog;
    private int mIndex = 0;
    private static final String IMAGE_FILE_NAME = "faceImage";
    private ArrayList<UserBean> userBeans = DataCenter.userLogin;
    private EditText titleText;
    private EditText telephoneText;
    private EditText trueName;
    private EditText introduceText;
    private String title;
    private String catid = "57";
    private String truename;
    private String introduce;
    private String telephone;
    private TextView down;
    private WindowManager wm;
    private View view;
    private ScrollView scrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messagepublic_main);
        titleText = (EditText) findViewById(R.id.titles);
        telephoneText = (EditText) findViewById(R.id.telephone);
        trueName = (EditText) findViewById(R.id.usernames);
        introduceText = (EditText) findViewById(R.id.introduce);
        inflater = LayoutInflater.from(this);
        down = (TextView) findViewById(R.id.down);
        text = (TextView) findViewById(R.id.text);
        textView = (TextView) findViewById(R.id.textView);
        initDialog();
        findView();
        initPopupWindow();
        initImageListener();
        scrollView = (ScrollView) findViewById(R.id.scrollView);
        AppUtils.setupUI(scrollView, this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        Log.d("WindowManager", text.getWidth() + "   " + down.getWidth());
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LinearLayout);
        Log.d("WindowManager", wm.getDefaultDisplay().getWidth() + "    " + linearLayout.getWidth());

        int width = (wm.getDefaultDisplay().getWidth() - text.getWidth() - down.getWidth() - (wm.getDefaultDisplay().getWidth() - linearLayout.getWidth()));
        int height = wm.getDefaultDisplay().getHeight();
        //Here you can get the size!
        popupWindow = new PopupWindow(view);
        popupWindow.setWidth(width);
        popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
//        popupWindow.setFocusable(true);
    }

    public void findView() {
        wm = this.getWindowManager();
        view = inflater.inflate(R.layout.messagepublic_item, null);
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.title.setText("信息矿发布");
        actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon2.setImageResource(R.drawable.favorites_complete);
        actionBarView.button_right_icon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (DataCenter.userLogin.size() == 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MessagePublish.this).setTitle("错误信息").setMessage("请完善您的用户信息").setPositiveButton("确定", null);
                    return;
                } else {
                    actionBarView.button_right_icon2.setEnabled(false);
                    cheakUpdate();
                }
            }
        });
        jobs = (TextView) view.findViewById(R.id.jobs);
        jobs.setOnClickListener(this);
        house = (TextView) view.findViewById(R.id.house);
        house.setOnClickListener(this);
        market = (TextView) view.findViewById(R.id.market);
        market.setOnClickListener(this);
        life = (TextView) view.findViewById(R.id.life);
        life.setOnClickListener(this);
        education = (TextView) view.findViewById(R.id.education);
        education.setOnClickListener(this);
        love = (TextView) view.findViewById(R.id.love);
        love.setOnClickListener(this);
        health = (TextView) view.findViewById(R.id.health);
        health.setOnClickListener(this);
        mImg0 = (ImageView) findViewById(R.id.auction_upload_imageView);
        mImg1 = (ImageView) findViewById(R.id.auction_upload_imageView1);
        mImg2 = (ImageView) findViewById(R.id.auction_upload_imageView2);
        mImg3 = (ImageView) findViewById(R.id.auction_upload_imageView3);
        mImg4 = (ImageView) findViewById(R.id.auction_upload_imageView4);
        mImg5 = (ImageView) findViewById(R.id.auction_upload_imageView5);
        mImg6 = (ImageView) findViewById(R.id.auction_upload_imageView6);
        mImg7 = (ImageView) findViewById(R.id.auction_upload_imageView7);
    }

    private void cheakUpdate() {
        // 品牌
        title = titleText.getText().toString();
        telephone = telephoneText.getText().toString();
        truename = trueName.getText().toString();
        introduce = introduceText.getText().toString();
        Log.d("brand", title);
        if (!Validate.search(title)) {
            Toast.makeText(this, "标题为3-30字，不能输入空格", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        }
        if (isEmpty(title)) {
            Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        }
        if (title.length() > 30 || title.length() < 3) {
            Toast.makeText(this, "标题为3-30字，不能输入空格", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        }
        Log.d("brand", truename);
        if (isEmpty(truename)) {
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        } else if (!Validate.truenameValidate(truename)) {
            Toast.makeText(this, "姓名为2-5位，不能输入空格", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        }

        // 名称
        Log.d("brand", telephone);
        if (isEmpty(telephone)) {
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        }
        // 几成新
        Log.d("brand", title);
        if (isEmpty(introduce)) {
            Toast.makeText(this, "介绍不能为空", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        } else if (introduce.length() <= 2) {
            Toast.makeText(this, "介绍字数应为2个以上", Toast.LENGTH_SHORT).show();
            actionBarView.button_right_icon2.setEnabled(true);
            return;
        }
//        Log.d("brand", mImagePaths[0].toString()+"");
        if (mImagePaths[0] != null && !"".equals(mImagePaths[0]) && new File(mImagePaths[0]).exists()) {
            // 开始上传图片
            uploadImages(0);
        } else {
            // ##可以上传了
            startUpLoadData();
        }

    }

    private boolean isEmpty(String str) {
        if (str != null && !"".equals(str.trim())) {
            return false;
        }
        return true;
    }

    public void initPopupWindow() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.types);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    Log.d("MessagePublish", popupWindow.isShowing() + "-----");
                    popupWindow.showAsDropDown(view);
                    popupWindow.update();
                    Log.d("MessagePublish", popupWindow.isShowing() + "-----");
                }
            }
        });
    }

    private void uploadImages(final int pos) {

        RequestParams params = new RequestParams();
        params.put("moduleid", "30");

        try {
            params.put("upalbum", new File(mImagePaths[pos]));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
//    	params.put("oldurl", values); 	// 需要判断

        RequestClient.post(this, "http://www.3tixa.com:8086/kaiyi/appupload.php", params, new AsyncHttpResponseParse(this, new LoadDataHandler() {

            @Override
            public void onStart() {
                mLoadingDialog.show();
            }

            @Override
            public void onSuccess(String data) {
                Log.d("OnSuccess", data + "------------");
                if (data != null) {
                    Log.d("OnSuccess", data + "+++++++++");
                    mImagePaths[pos] = data.substring(2, data.length() - 2);
                    System.out.println(pos + ":" + data.substring(2, data.length() - 2));
                }
                // 提交成功, 提交下一个
                if (pos < mImagePaths.length - 1) {
                    if (mImagePaths[pos + 1] != null && !"".equals(mImagePaths[pos + 1])) {
                        uploadImages(pos + 1);
                    } else {
                        // 图片上传完毕
                        /***开始上传信息**/
                        startUpLoadData();
                    }
                } else {
                    // 图片上传完毕
                    /***开始上传信息**/
                    startUpLoadData();
                }
            }

            @Override
            public void onFailure(Throwable error, String content) {
                // 失败重新提交
               // uploadImages(pos);
                actionBarView.button_right_icon2.setEnabled(true);
                mLoadingDialog.dismiss();
                Toast.makeText(MessagePublish.this, "发布失败，请重试！", Toast.LENGTH_SHORT).show();
                return;
            }

        }));
    }

    private void initImageListener() {
        mImg0.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 0;
                showDialogOrFullImage(mIndex);
            }
        });
        mImg1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 1;
                showDialogOrFullImage(mIndex);
            }
        });
        mImg2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 2;
                showDialogOrFullImage(mIndex);
            }
        });
        mImg3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 3;
                showDialogOrFullImage(mIndex);
            }
        });
        mImg4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 4;
                showDialogOrFullImage(mIndex);
            }
        });
        mImg5.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 5;
                showDialogOrFullImage(mIndex);
            }
        });
        mImg6.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 6;
                showDialogOrFullImage(mIndex);
            }
        });
        mImg7.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mIndex = 7;
                showDialogOrFullImage(mIndex);
            }
        });
    }

    private void showDialogOrFullImage(int mIndex) {
        if (mImagePaths == null || mImagePaths[mIndex] == null || TextUtils.isEmpty(mImagePaths[mIndex].trim())) {
            showImageDialog();
        } else {
            startFullImage(mIndex);
        }
    }

    private void startFullImage(int defaultIndex) {
        Intent intent = new Intent(MessagePublish.this, ImageGalleryActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("defaultIndex", defaultIndex);
        ArrayList<String> imgPaths = new ArrayList<String>();
        for (String path : mImagePaths) {
            if (!TextUtils.isEmpty(path)) {
                imgPaths.add(path);
            }
        }
        Log.e("imgPaths", imgPaths.get(0));
        bundle.putStringArrayList("imgPaths", imgPaths);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    // 上传图片
    private void showImageDialog() {
        new AlertDialog.Builder(this)
                .setTitle("上传图片")
                .setItems(items, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                Intent intentFromGallery = new Intent();
                                intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
                                intentFromGallery.setType("image/*"); // 设置文件类型
                                startActivityForResult(intentFromGallery, UploadImageUtils.IMAGE_REQUEST_CODE);
                                break;
                            case 1:
                                Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                // 判断存储卡是否可以用，可用进行存储
                                if (AppUtils.isExistSD()) {
                                    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                                    File file = new File(path + "/" + IMAGE_FILE_NAME + mIndex + ".png");
                                    if (!file.exists()) {
                                        file.getParentFile().mkdirs();
                                    }
                                    intentFromCapture.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                                    intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                                }
                                startActivityForResult(intentFromCapture, UploadImageUtils.CAMERA_REQUEST_CODE);
                                break;
                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private String savePhoto(Bundle bundle) {
        if (bundle != null) {
            bitmap = bundle.getParcelable("data");
        } else {
            return null;
        }

        FileOutputStream b = null;
        String pathPhoto = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/cache/";
        File file = new File(pathPhoto + IMAGE_FILE_NAME + mIndex + ".png");
        if (!file.exists()) {
            file.getParentFile().mkdirs();// 创建文件夹
        }
        try {
            b = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (b != null) {
                    b.flush();
                    b.close();
                }
                return file.getAbsolutePath();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

private String picPath;
private Bitmap bitmap;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //结果码不等于取消时候
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
                case UploadImageUtils.IMAGE_REQUEST_CODE:
                    Log.e("switch",data.getData().toString());
                    String[] proj = { MediaStore.Images.Media.DATA };

                    Cursor actualimagecursor = managedQuery(data.getData(),proj,null,null,null);

                    int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                    actualimagecursor.moveToFirst();

                    String img_path = actualimagecursor.getString(actual_image_column_index);

                    File file = new File(img_path);
                    BitmapFactory.Options options1 =  new BitmapFactory.Options();
                    options1.inSampleSize = 4;
                    bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),options1);
//                    UploadImageUtils.startPhotoZoom(MessagePublish.this, data.getData());
                    data.putExtra("data",bitmap);
                    Log.d("img_path",img_path);
                    data.putExtra("img_path",img_path);
                    savePath(data);
                    break;
                case UploadImageUtils.CAMERA_REQUEST_CODE:
                    if (AppUtils.isExistSD()) {
                            Log.e("SWITCH",data+"");
                            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                            File tempFile = new File(path + "/" +IMAGE_FILE_NAME + mIndex + ".png");
                            Log.e("switch",IMAGE_FILE_NAME + "－－－－" + mIndex);
//                        UploadImageUtils.startPhotoZoom(MessagePublish.this, Uri.fromFile(tempFile));
                            Log.e("Path",path.toString());
                            Log.e("tempFile",tempFile.getAbsolutePath()+"");
                            options1 =  new BitmapFactory.Options();
                            options1.inSampleSize = 4;
                            bitmap = BitmapFactory.decodeFile(tempFile.getAbsolutePath(),options1);
//                    UploadImageUtils.startPhotoZoom(MessagePublish.this, data.getData());
//                            data.putExtra("data",bitmap);
                            Log.d("img_path",tempFile.getAbsolutePath());
//                            data.putExtra("img_path",tempFile.getAbsolutePath());
                            picPath = tempFile.getAbsolutePath();
                            savePath(data);
                    } else {
                        Toast.makeText(MessagePublish.this, "未找到存储卡，无法存储照片！", Toast.LENGTH_LONG).show();
                    }
                    break;
                case UploadImageUtils.RESULT_REQUEST_CODE: //图片缩放完成后

                    break;
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        if(bitmap!=null){
            bitmap.recycle();
        }
        super.onDestroy();
    }


    Drawable drawable = null;
    public void savePath(Intent data){
        if (data != null) {
            // 保存图片
            mImagePaths[mIndex] = savePhoto(data.getExtras());
            if( mImagePaths[mIndex] == null ) {
                Toast.makeText(this, "图片转换出错", Toast.LENGTH_SHORT).show();
                return;
            }
            Log.e("switch",mIndex + "-----" + data.getExtras());
            Log.e("switch",mImagePaths[mIndex] + "-----" );
            Drawable drawable = UploadImageUtils.getImageToView(data, MessagePublish.this);
            switch (mIndex) {
                case 0:
                    mImg0.setImageDrawable(drawable);
                    mImg1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    mImg1.setImageDrawable(drawable);
                    mImg2.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    mImg2.setImageDrawable(drawable);
                    mImg3.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    mImg3.setImageDrawable(drawable);
                    mImg4.setVisibility(View.VISIBLE);
                    break;
                case 4:
                    mImg4.setImageDrawable(drawable);
                    mImg5.setVisibility(View.VISIBLE);
                    break;
                case 5:
                    mImg5.setImageDrawable(drawable);
                    mImg6.setVisibility(View.VISIBLE);
                    break;
                case 6:
                    mImg6.setImageDrawable(drawable);
                    mImg7.setVisibility(View.VISIBLE);
                    break;
                case 7:
                    mImg7.setImageDrawable(drawable);
                    break;
                default:
                    break;
            }
        }else {
            // 保存图片
            mImagePaths[mIndex] = picPath;
            if( mImagePaths[mIndex] == null ) {
                Toast.makeText(this, "图片转换出错", Toast.LENGTH_SHORT).show();
                return;
            }
            Log.e("switch",mIndex + "-----" );
            Log.e("switch",mImagePaths[mIndex] + "-----" );
//            Drawable drawable = UploadImageUtils.getImageToView(data, MessagePublish.this);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Bitmap photo = BitmapFactory.decodeFile(picPath, options);

            drawable = new BitmapDrawable(this.getResources(),photo);
            switch (mIndex) {
                case 0:
                    mImg0.setImageDrawable(drawable);
                    mImg1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    mImg1.setImageDrawable(drawable);
                    mImg2.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    mImg2.setImageDrawable(drawable);
                    mImg3.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    mImg3.setImageDrawable(drawable);
                    mImg4.setVisibility(View.VISIBLE);
                    break;
                case 4:
                    mImg4.setImageDrawable(drawable);
                    mImg5.setVisibility(View.VISIBLE);
                    break;
                case 5:
                    mImg5.setImageDrawable(drawable);
                    mImg6.setVisibility(View.VISIBLE);
                    break;
                case 6:
                    mImg6.setImageDrawable(drawable);
                    mImg7.setVisibility(View.VISIBLE);
                    break;
                case 7:
                    mImg7.setImageDrawable(drawable);
                    break;
                default:
                    break;
            }
        }
    }


    private void startUpLoadData() {
        RequestParams params = new RequestParams();
        params.put("mid", "30");
        params.put("action", "add");
        params.put("userid", userBeans.get(0).getUserid());
        params.put("post[catid]", catid + "");
        params.put("post[title]", title);
        params.put("post[username]", truename);
        params.put("post[introduce]", introduce);
        params.put("post[telephone]", telephone);

        for (int i = 0; i < mImagePaths.length; i++) {
            if (mImagePaths[i] != null && !"".equals(mImagePaths[i].trim())) {
                if (i == 0) {
                    params.put("post[thumb]", mImagePaths[i]);
                } else {
                    params.put("post[thumb" + i + "]", mImagePaths[i]);
                }
            }
        }

        System.out.println("http://www.3tixa.com:8086/kaiyi/member/my.php   " + params.toString());

        RequestClient.post(this, "http://www.3tixa.com:8086/kaiyi/member/my.php", params, new AsyncHttpResponseHandler() {
            //192.168.29.135:8080
//www.3tixa.com:8086
            @Override
            public void onStart() {
                if (!mLoadingDialog.isShowing()) {
                    mLoadingDialog.show();
                }
            }

            @Override
            public void onSuccess(String content) {
                if (!HttpUtils.isConnected()) {
                    showToast(R.string.no_network);
                    return;
                } else {
                    String code = "0";
                    String msg = "";
                    try {
                        JSONObject o = new JSONObject(content);
                        msg = o.getString("msg");
                        code = o.getString("code");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (code.equals("1")) {
                        Toast.makeText(MessagePublish.this, "发布成功", Toast.LENGTH_SHORT).show();
//                    Intent intent = new Intent(MessagePublish.this,MessageInfo.class);
//                    startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(MessagePublish.this, msg, Toast.LENGTH_SHORT).show();
                        return;
                    }
                }

                System.out.println("content-->" + content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                actionBarView.button_right_icon2.setEnabled(true);
                Toast.makeText(MessagePublish.this, "发布失败，请重试！", Toast.LENGTH_SHORT).show();
                return;
            }

            @Override
            public void onFinish() {
                actionBarView.button_right_icon2.setEnabled(true);
                if (mLoadingDialog != null) {
                    mLoadingDialog.dismiss();
                }
            }

        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jobs:
                textView.setText(types[0]);
                Log.d("MessagePublish", popupWindow + "");
                if (popupWindow != null) {
                    this.popupWindow.dismiss();
                }
                catid = "57";
                break;
            case R.id.house:
                Log.d("MessagePublish", popupWindow.isShowing() + "");
                if (this.popupWindow.isShowing()) {
                    this.popupWindow.dismiss();
                }
                Log.d("MessagePublish", popupWindow.isShowing() + "");
                textView.setText(types[1]);
                catid = "58";
                break;
            case R.id.market:
                textView.setText(types[2]);
                popupWindow.dismiss();
                catid = "59";
                break;
            case R.id.life:
                textView.setText(types[3]);
                popupWindow.dismiss();
                catid = "60";
                break;
            case R.id.education:
                textView.setText(types[4]);
                popupWindow.dismiss();
                catid = "61";
                break;
            case R.id.love:
                textView.setText(types[5]);
                popupWindow.dismiss();
                catid = "62";
                break;
            case R.id.health:
                textView.setText(types[6]);
                popupWindow.dismiss();
                catid = "63";
                break;
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        if (mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    private void initDialog() {
        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setText("提交数据中...");
        mLoadingDialog.dismiss();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

}
